#include "ChannelSelect.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QListView>
#include <QLabel>

ChannelSelectWidget::ChannelSelectWidget(QWidget *parent) : QWidget(parent)
{
    Init();
}

void ChannelSelectWidget::SetPoints(const QList<QSharedPointer<Point>> &points)
{
    ClearListWidget();
    for (auto point : points)
    {
        auto item = new QListWidgetItem(point->GetName());
        item->setData(Qt::UserRole, point->GetId());
        showListWidget->addItem(item);
    }
}

void ChannelSelectWidget::Init()
{
    setFixedSize(800, 600);
    setWindowTitle("select channel");

    auto mainLayout = new QHBoxLayout;
    mainLayout->setMargin(20);
    mainLayout->setSpacing(0);

    InitHideWidget();
    InitOptWidget();
    InitShowWidget();

    mainLayout->addWidget(hideWidget);
    mainLayout->addWidget(optWidget);
    mainLayout->addWidget(showWidget);
    setLayout(mainLayout);
}

void ChannelSelectWidget::InitHideWidget()
{
    hideWidget = new QWidget(this);
    auto hideLayout = new QVBoxLayout;
    auto nameLabel = new QLabel("隐藏的通道", this);
    hideListWidget = new QListWidget(this);
    hideLayout->addWidget(nameLabel);
    hideLayout->addWidget(hideListWidget);
    hideWidget->setLayout(hideLayout);
}

void ChannelSelectWidget::InitOptWidget()
{
    optWidget = new QWidget(this);
    optWidget->setFixedWidth(120);
    auto centerLayout = new QVBoxLayout;
    auto optLayout = new QVBoxLayout;
    addButton = new QPushButton("+ >>", this);
    addButton->setFixedSize(100, 28);
    subtractButton = new QPushButton("- <<", this);
    subtractButton->setFixedSize(100, 28);
    optLayout->addWidget(addButton);
    optLayout->addWidget(subtractButton);
    confirmButton = new QPushButton("确定", this);
    confirmButton->setFixedSize(100, 28);
    centerLayout->addStretch();
    centerLayout->addLayout(optLayout);
    centerLayout->addStretch();
    centerLayout->addWidget(confirmButton);
    optWidget->setLayout(centerLayout);

    connect(addButton, &QPushButton::clicked, this, &ChannelSelectWidget::SlotAddButtonClicked);
    connect(subtractButton, &QPushButton::clicked, this, &ChannelSelectWidget::SlotSubtractButtonClicked);
    connect(confirmButton, &QPushButton::clicked, this, &ChannelSelectWidget::SlotConfirmButtonClicked);
}

void ChannelSelectWidget::InitShowWidget()
{
    showWidget = new QWidget(this);
    auto showLayout = new QVBoxLayout;
    auto nameLabel = new QLabel("显示的通道", this);
    showListWidget = new QListWidget(this);
    showLayout->addWidget(nameLabel);
    showLayout->addWidget(showListWidget);
    showWidget->setLayout(showLayout);
}

void ChannelSelectWidget::ClearListWidget()
{
    hideListWidget->clear();
    showListWidget->clear();
}

void ChannelSelectWidget::SlotAddButtonClicked()
{
    auto row = hideListWidget->currentRow();
    auto hideItem = hideListWidget->takeItem(row);
    if (hideItem == nullptr)
    {
        return;
    }

    auto showItem = new QListWidgetItem(hideItem->text());
    showItem->setData(Qt::UserRole, hideItem->data(Qt::UserRole));
    showListWidget->addItem(showItem);
}

void ChannelSelectWidget::SlotSubtractButtonClicked()
{
    auto row = showListWidget->currentRow();
    auto showItem = showListWidget->takeItem(row);
    if (showItem == nullptr)
    {
        return;
    }

    auto hideItem = new QListWidgetItem(showItem->text());
    hideItem->setData(Qt::UserRole, showItem->data(Qt::UserRole));
    hideListWidget->addItem(hideItem);
}

void ChannelSelectWidget::SlotConfirmButtonClicked()
{
    close();
    QStringList ids;
    for (int i = 0; i < showListWidget->count(); i++)
    {
        auto id = showListWidget->item(i)->data(Qt::UserRole).toString();
        ids.push_back(id);
    }
    emit SignalShowPoints(ids);
}
